<?php
/**
 * 数据字典管理
 * @author hushuai
 *
 */
class DictAction extends CommonAction {
 
	private $Dict  = "Dict";    //定义数据字典数据表
	
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 根据id查询
     * @access public
     * @param  int    $id  
     * @return array
     */
    public function find(){
    
    	$id = $this->_req("id","intval",0);
    	
    	$data  = M($this->Dict)->find($id);
    
    	$this->success($data);
    }
    
    /**
     * 查询所有数据字典
     * @access public
     * @return array
     */
    public function select(){
    	 
    	$data  = M($this->Dict)->order("sort desc")->select();
    
    	$this->success($data);
    }
    /**
     * 读取数据字典的一级栏目
     * @access public
     * @return array
     */
    public function a_column(){
    
    	$map['level'] = 1;
    	$data  = M($this->Dict)->where($map)->order("sort desc")->select();
    	//echo M($this->Dict)->getLastSql();
    	$this->success($data);
    }
    /**
     * 根据选择的父级栏目查询相应的子栏目
     * @param  int    $id
     * @access public
     * @return array
     */
    public function getChildColumn(){
    
    	$key = $this->_req("key");
    	
    	$map['key']    = $key;
    	
    	$data  = M($this->Dict)->where($map)->order("sort desc")->select();
    	//echo M($this->Dict)->getLastSql();
    	$this->success($data);
    }
    
    /**
     * 增加字典
     * @param  int    $id
     * @access public
     * @return array
     */
    public function add(){
    	
    	$param = array(
    			
    		"before" => "dict_add_before",
    		"after"  => "dict_add_after",
    	);
    	parent::do_add($this->Dict,$param);
    	
    }
    /**
     * 增加字典
     * @param  array
     * @access public
     * @return array
     */
    public function edit(){
    	 
    	$param = array(
    		"before" => "dict_edit_before",
    		"after"  => "dict_add_after",
    	);
    	parent::do_edit($this->Dict,$param);
    	 
    }
    
    /**
     * 排序
     * @param  array
     * @access public
     * @return array
     */
    public function sort(){
    	
    	$model  = D($this->Dict);
    	$sorts  = $this->_req("sort");
    	
    	foreach ($sorts as $id => $sort) {
    		$data = $model->save(array("id"=>$id,"sort"=>$sort));
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->success("修改失败");
    	}
    	
    }
    
    public function del(){
    	
    	$model  = D($this->Dict);
    	
    	$id     = $this->_req('id',"intval","0");
    	$rec    = $model->find($id);
    	$res1   = $model->delete($id);
    		
    	$model->updLeaf($rec["pid"],true);
    	$model->updTotal($rec['key']);
    	$model->getDictByKey($rec['key']);
    		
    	if($res1){
    		//wlog(1);
    		$this->success($res1);
    	}else{
    		//wlog(2);
    		$this->error("删除失败！");
    	}
    }
}